import React, { Component } from 'react';
import { Modal, Form, message, Radio, Button, Icon, Input, Select } from 'antd';

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
export default class NewForm extends Component {
  state = {
    value: 1,
  };

  // onChange = e => {
  //   //console.log('radio checked', e.target.value);
  //   this.setState({
  //     value: e.target.value,
  //   });
  // };
  submit = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        //console.log('Received values of form: ', values);
        const progSize = `${values.width}*${values.height}`;
        this.props.dispatch({
          type: 'program/add',
          payload: { ...values, progSize },
          callback: () => {
            message.success('添加节目成功.');

            this.props.handleModalVisible(false);
            this.props.form.resetFields();
            //this.props.handleSearch();

            // this.props.dispatch({
            //   type: 'program/fetch',
            //   payload: { page: 1, pageSize: 10 },
            // });
          },
        });
      }
    });
  };

  render() {
    const { modalVisible, form, handleModalVisible, progType } = this.props;
    return (
      <Modal
        title="新建节目"
        forceRender
        visible={modalVisible}
        onOk={this.submit}
        onCancel={() => handleModalVisible(false)}
      >
        <Form>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="名称">
            {form.getFieldDecorator('progName', {
              rules: [
                {
                  required: true,
                  message: '请勿使用特殊字符',
                  pattern: RegExp('^[\u4e00-\u9fa5_a-zA-Z0-9]+$'),
                },
              ],
            })(<Input placeholder="请使用汉字、字母和数字" />)}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="作者">
            {form.getFieldDecorator('progAuthor', {
              rules: [{ required: true, message: '请输入作者...' }],
            })(<Input placeholder="请输入" />)}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="类型">

            {form.getFieldDecorator('progType')(
              <Select allowClear="true" placeholder="节目类型" style={{ width: '100%' }}>
                {progType.map((item, index) => (
                  <Option value={item.code}>{item.name}</Option>
                ))}
              </Select>
            )}

          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="宽">
            {form.getFieldDecorator('width', {
              rules: [{ required: true, message: '请输入节目的宽...' }],
            })(<Input placeholder="宽" />)}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="高">
            {form.getFieldDecorator('height', {
              rules: [{ required: true, message: '请输入节目的高...' }],
            })(<Input placeholder="高" />)}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}
